const { register, login } = require('../../Dao/people/loginDao');
const bcryptjs = require('bcryptjs')
const jwt = require('jsonwebtoken')

module.exports.register = async function (user) {
    const data = await register(user);
   // console.log('第二层注册返回值',data);
    if (data) {
       return success = true;
    } else {
        return success=false;
    }
}

module.exports.login = async function ({ loginName, loginPass }) {
    const [data] = await login({ loginName });
    if (data) {
        const result = bcryptjs.compareSync(loginPass, data.password);
        if (result) {
            const token = jwt.sign(
                { loginName },
                'abc',
                { expiresIn: 60 * 60 }
            )
            return {
                msg: '登录成功',
                success: true,
                token
            } 
        }
        else {
            return {
                msg: '密码错误',
                success: false
            }
        }
    } else {
        return {
            msg: '用户名不正确',
            success: false
        }
    }
}